<?php

/**
 * attendance actions.
 *
 * @package    mysmartmanager
 * @subpackage attendance
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class attendanceActions extends sfActions
{
  public function executeMark(sfWebRequest $request)
  {    
	$attendances = Doctrine::getTable('Attendance')
      ->createQuery('a')
	  ->where('a.user_id = ?', sfContext::getInstance()->getUser()->getResourceId())
	  ->andwhere ('DATEDIFF(a.punch_time, SYSDATE()) = ?', 0)
  	  ->orderBy ('a.punch_time DESC')
	  ->limit (1)
      ->execute();

	$this->attendance = new Attendance();
	if (count($attendances) == 0)
	{		  	  
	  $this->attendance['punch_type'] = true;
	}
	else
	{
	  $attendanceRow = $this->attendances[0];
	  $this->attendance['punch_type'] = !$attendanceRow['punch_type'];
	}

	$this->attendance['punch_time'] = date('Y-m-d H:i:s'); 
	$this->attendance['user_id'] = sfContext::getInstance()->getUser()->getResourceId();

	$this->form = new AttendanceForm($this->attendance);
	//$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
	if ($this->form->isvalid())
	{
	  $this->form->save();
	  $this->getUser()->setFlash('notice', 'Your Punch '.($this->attendance['punch_type']?'In':'Out').'In Time: ' . date('H:i:s', strtotime($this->attendance['punch_time'])));
  
	}
	  
    $this->redirect('@homepage');
  }
  public function executeOkay(sfWebRequest $request)
  {
    
  }
  public function executeIndex(sfWebRequest $request)
  {  
    $resourceId = sfContext::getInstance()->getUser()->getResourceId();
	if (!$resourceId)
	  $this->redirect('resource/new');
	$this->attendances = Doctrine::getTable('Attendance')
      ->createQuery('a')
	  ->where('a.user_id = ?', $resourceId)
      ->execute();
	if (count($this->attendances) == 0)
	{
	  sfContext::getInstance()->getUser()->setFlash('error','No Attendance Record');
	  $this->redirect('attendance/new');
	}
	foreach ($this->attendances as $attendanceRow):
	  //something here 
	endforeach;
  }

  public function executeShow(sfWebRequest $request)
  {
    if (!sfContext::getInstance()->getUser()->getResourceId())
	  $this->redirect('resource/new');
	$this->attendance = Doctrine::getTable('Attendance')->find(array($request->getParameter('attendanceid')));
    $this->forward404Unless($this->attendance);
  }

  public function executeNew(sfWebRequest $request)
  {
    if (!sfContext::getInstance()->getUser()->getResourceId())
	  $this->redirect('resource/new');
	$attendances = Doctrine::getTable('Attendance')
      ->createQuery('a')
	  ->where('a.user_id = ?', sfContext::getInstance()->getUser()->getResourceId())
	  ->andwhere ('DATEDIFF(a.punch_time, SYSDATE()) = ?', 0)
  	  ->orderBy ('a.punch_time DESC')
	  ->limit (1)
      ->execute();
   

	$this->attendance = new Attendance();
	if (count($attendances) == 0)
	{		  	  
	  $this->attendance['punch_type'] = true;
	}
	else
	{
	  $attendanceRow = $attendances->get(0);	  
	  $this->attendance['punch_type'] = ($attendanceRow['punch_type']==1? false: true);
	  
	}
	//$this->attendance['punch_type'] = true;
	$this->form = new AttendanceForm();
	$this->form->setDefault('punch_type', $this->attendance['punch_type']);
	$this->form->setDefault('punch_time', date('Y-m-d H:i:s'));
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new AttendanceForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    if (!sfContext::getInstance()->getUser()->getResourceId())
	  $this->redirect('resource/new');
	$this->forward404Unless($attendance = Doctrine::getTable('Attendance')->find(array($request->getParameter('attendanceid'))), sprintf('Object attendance does not exist (%s).', $request->getParameter('attendanceid')));
    $this->form = new AttendanceForm($attendance);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($attendance = Doctrine::getTable('Attendance')->find(array($request->getParameter('attendanceid'))), sprintf('Object attendance does not exist (%s).', $request->getParameter('attendanceid')));
    $this->form = new AttendanceForm($attendance);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($attendance = Doctrine::getTable('Attendance')->find(array($request->getParameter('attendanceid'))), sprintf('Object attendance does not exist (%s).', $request->getParameter('attendanceid')));
    $attendance->delete();

    $this->redirect('attendance/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
	
    if ($form->isValid())
    {
      $attendance = $form->save();	  
      $this->redirect('attendance/index');
    }
  }
}
